package best.driver.android.Activity.Finance;

import android.content.Intent;
import android.os.Bundle;
import android.util.Pair;
import android.view.View;
import android.widget.*;
import best.driver.android.Activity.Base.BaseActivity;
import best.driver.android.Beans.IncomeStatisticsInfo;
import best.driver.android.R;
import best.driver.android.RequestFactory;
import best.driver.android.Utils.TimeUtil;
import best.driver.android.Utils.Util;
import best.driver.android.common.app.AppProxyFactory;
import best.driver.android.common.network.ApiObjectRequest;
import best.driver.android.common.network.ApiRequest;
import best.driver.android.common.network.BestError;
import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

import java.util.Calendar;

public class IncomeStatisticsActivity extends BaseActivity {

    private final String DAILY = "DAILY";
    private final String WEEKLY = "WEEKLY";
    private final String MONTHLY = "MONTHLY";
    private final String TOTAL = "TOTAL";
    private final String[] REPORT_TYPE = new String[] {TOTAL, DAILY, WEEKLY, MONTHLY};

    private String mType = TOTAL;

    View mViews[] = new View[4];
    RadioButton mRbs[] = new RadioButton[4];

    private int mSelectIndex = 6;

    @InjectView(R.id.rg_record)
    RadioGroup mRgRecord;

    @InjectView(R.id.fl_month_selector)
    FrameLayout mFlMonthSelector;

    @InjectView(R.id.iv_left)
    ImageView mIvLeft;

    @InjectView(R.id.iv_right)
    ImageView mIvRight;

    @InjectView(R.id.tv_time)
    TextView mTvTime;

    @InjectView(R.id.tv_total_count)
    TextView mTvTotalCount;

    @InjectView(R.id.tv_cash_count)
    TextView mTvCashCount;

    @InjectView(R.id.tv_online_count)
    TextView mTvOnlineCount;

    @InjectView(R.id.ll_income)
    LinearLayout mLlIncome;

    @InjectView(R.id.tv_income_cash)
    TextView mTvIncomeCash;

    @InjectView(R.id.tv_income_online)
    TextView mTvIncomeOnline;

    @InjectView(R.id.tv_income_subsidy)
    TextView mTvIncomeSubsidy;

    @InjectView(R.id.tv_income_total)
    TextView mTvIncomeTotal;

    @OnClick(R.id.iv_left)
    protected void selectPreMonth() {
        mSelectIndex--;
        mIvRight.setVisibility(View.VISIBLE);
        if(mSelectIndex == 1) {
            mIvLeft.setVisibility(View.GONE);
        }
        Pair<Long, String> pair = getMonthTimeMillisPair(mSelectIndex);
        mTvTime.setText(pair.second);
        getIncomeStatistics(MONTHLY, pair.first, mSelectIndex);
    }

    @OnClick(R.id.iv_right)
    protected void selectNextMonth() {
        mSelectIndex++;
        mIvLeft.setVisibility(View.VISIBLE);
        if(mSelectIndex == 6) {
            mIvRight.setVisibility(View.GONE);
        }
        Pair<Long, String> pair = getMonthTimeMillisPair(mSelectIndex);
        mTvTime.setText(pair.second);
        getIncomeStatistics(MONTHLY, pair.first, mSelectIndex);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setView(R.layout.activity_income_record);
        ButterKnife.inject(this, view);
        setLeftIvBak(R.drawable.icon_navibar_back);
        setRightIvBak(R.drawable.icon_rule);
        mViews[0] = view.findViewById(R.id.view_1);
        mViews[1] = view.findViewById(R.id.view_2);
        mViews[2] = view.findViewById(R.id.view_3);
        mViews[3] = view.findViewById(R.id.view_4);
        mRbs[0] = (RadioButton) view.findViewById(R.id.rb_record_all);
        mRbs[1] = (RadioButton) view.findViewById(R.id.rb_record_today);
        mRbs[2] = (RadioButton) view.findViewById(R.id.rb_record_week);
        mRbs[3] = (RadioButton) view.findViewById(R.id.rb_record_month);
        mFlMonthSelector.setVisibility(View.GONE);
        mIvRight.setVisibility(View.GONE);
        setTopLineVisible(false);
        mTvTime.setText(getMonthTimeMillisPair(mSelectIndex).second);

        mRgRecord.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int index) {

                    for(int i = 0; i < 4; i++) {
                        if(mRbs[i].getId() == index) {
                            mViews[i].setBackgroundResource(R.color.white);
                            mFlMonthSelector.setVisibility(i == 3 ? View.VISIBLE : View.GONE);
                            long time = (i == 3 ? getMonthTimeMillisPair(mSelectIndex).first : System.currentTimeMillis());
                            getIncomeStatistics(REPORT_TYPE[i], time, mSelectIndex);
                        } else {
                            mViews[i].setBackgroundResource(R.color.best_blue);
                        }
                    }


            }
        });
        setTopTopic("收入统计");
        getIncomeStatistics(TOTAL, System.currentTimeMillis(), mSelectIndex);
    }


    @Override
    protected void onBack() {
        this.finish();
    }

    @Override
    protected void onclickRightIv() {
        super.onclickRightIv();
        Intent intent = new Intent(IncomeStatisticsActivity.this, IncomeRuleActivity.class);
        startActivity(intent);
    }

    private Pair<Long, String> getMonthTimeMillisPair(int index) {
        Calendar c = Calendar.getInstance();
        c.add(Calendar.MONTH, index - 6);
        long time = c.getTimeInMillis();
        return new Pair<Long, String>(time, TimeUtil.convertToYM(time));
    }

    private void getIncomeStatistics(final String type, long time, final int index) {
        mType = type;
        mLlIncome.setVisibility(View.GONE);
        showWaiting();
        ApiObjectRequest<IncomeStatisticsInfo> request = RequestFactory.createGetIncomeStatisticsRequest(type, time);
        request.setListener(new ApiRequest.ApiRequestListener<IncomeStatisticsInfo>() {
            @Override
            public void onRequestSuccess(IncomeStatisticsInfo response) {
                boolean isLast = false;
                if(type.equals(mType) && !mType.equals(REPORT_TYPE[3])) {
                    hideWaiting();
                    isLast = true;
                } else if(type.equals(mType) && mType.equals(REPORT_TYPE[3]) && mSelectIndex == index) {
                    hideWaiting();
                    isLast = true;
                }
                if(isLast) {
                    showDetail(response);
                }
            }

            @Override
            public void onRequestError(BestError error) {
                if(type.equals(mType) && !mType.equals(REPORT_TYPE[3])) {
                    hideWaiting();
                } else if(type.equals(mType) && mType.equals(REPORT_TYPE[3]) && mSelectIndex == index) {
                    hideWaiting();
                }
            }
        });
        AppProxyFactory.getProxy().getNetworkManager().enqueueRequest(request);
    }

    private void showDetail(IncomeStatisticsInfo info) {
        if(info == null) {
            info = new IncomeStatisticsInfo();
        }
        mLlIncome.setVisibility(View.VISIBLE);
        mTvCashCount.setText(info.cashOrderCount+"单");
        mTvOnlineCount.setText(info.onlineOrderCount+"单");
        mTvTotalCount.setText(info.totalOrderCount+"单");
        mTvIncomeCash.setText(Util.getOnePointNumber(info.cashIncome));
        mTvIncomeOnline.setText(Util.getOnePointNumber(info.onlineIncome));
        mTvIncomeSubsidy.setText(Util.getOnePointNumber(info.orderSubsidy));
        mTvIncomeTotal.setText(Util.getOnePointNumber(info.totalIncome));
    }

}
